package com.fbs.wowo.forum.mapper;

import com.fbs.wowo.forum.entity.Likes;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface LikesMapper {

    @Delete({
        "delete from likes",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);

    @InsertProvider(type=LikesSqlProvider.class, method="insertSelective")
    int insertSelective(Likes record);

    @Select({
        "select",
        "id, like_entity, like_type, user_id, status",
        "from likes",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="like_entity", property="likeEntity", jdbcType=JdbcType.VARCHAR),
        @Result(column="like_type", property="likeType", jdbcType=JdbcType.INTEGER),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    Likes selectByPrimaryKey(String id);

    @UpdateProvider(type=LikesSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Likes record);

    // 查询一条点赞
    @Select({
            "select",
            "id, like_entity, like_type, user_id, status",
            "from likes",
            "where like_entity = #{likeEntity, jdbcType=VARCHAR} and like_type = #{likeType, jdbcType=VARCHAR} and user_id = #{userId, jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="like_entity", property="likeEntity", jdbcType=JdbcType.VARCHAR),
            @Result(column="like_type", property="likeType", jdbcType=JdbcType.INTEGER),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.VARCHAR),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    Likes findByEntityAndUserId(@Param("likeEntity") String likeEntity, @Param("likeType") Integer likeType, @Param("userId") String userId);

    // 查询点赞数
    @Select({
            "select",
            "sum(status)",
            "from likes",
            "where like_entity = #{likeEntity, jdbcType=VARCHAR} and like_type = #{likeType, jdbcType=VARCHAR}"
    })
    Integer CountByEntity(@Param("likeEntity") String likeEntity, @Param("likeType") Integer likeType);
}